/*
Copyright 2005-2014 CP Fricano - All Rights Reserved

This software and source code is licensed under the Apache
License, Version 2.0. You may not use this software or source
code except in compliance with the License.  You may obtain a
copy of the License at: http://www.apache.org/licenses/LICENSE-2.0.
Use and distributed under this License is on an "as is" basis,
without expressed or implied warranties or conditions of any kind.
See the License for the specific language governing permissions and
limitations.
 */

package com.fricano.java42.instructional.example.swing;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

public class J42JPanel_02 extends JPanel {

    private static final long serialVersionUID = -42L;
    private int               degrees          = 0;

    public J42JPanel_02() {
    }

    @Override
    public void paintComponent(final Graphics g) {
        final Graphics2D g2d = (Graphics2D) g;
        if (degrees != 0) {
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
            g2d.rotate(Math.toRadians(degrees), getWidth() / 2, getHeight() / 2);
        }
        super.paintComponent(g);
    }

    public void doSpin(final int spinCount, final long delay, final int increment) {
        final Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int x = 0; x != spinCount; x++) {
                    for (degrees = 1; degrees < 360; degrees += increment) {
                        repaint();
                        try {
                            Thread.sleep(delay);
                        } catch (final InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                degrees = 0;
                repaint();
            }
        });
        t.setDaemon(true);
        t.start();
    }

}
